using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using SqlWebAdmin.CSharp;

public partial class Import : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    

    }

    protected void cmdImport_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
          
                lblError.Text = string.Empty;
                lblError.Visible = false;

				Server smoServer = SmoHelper.GetServer();
                try
                {
                    

                    System.IO.StreamReader stream = new System.IO.StreamReader(hifFileUpload.PostedFile.InputStream);
                    string sqlScript = stream.ReadToEnd();

                    if (sqlScript.Trim().Length > 0)
                    {
                        smoServer.ConnectionContext.ExecuteNonQuery(sqlScript);

                        lblImportSuccess.Visible = true;
                    }
                    else
                    {
                        lblError.Text = "Uploaded file is empty.";
                        lblError.Visible = true;
                    }
                }
                catch (SmoException smoEx)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                    lblError.Visible = true;
                }
                catch(Microsoft.SqlServer.Management.Common.ExecutionFailureException execEx)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(execEx));
                    lblError.Visible = false;
                }
                catch (System.Exception ex)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                    lblError.Visible = true;
                }
            
        }
    }
}
